package com.gljx.web.service.impl;

import com.gljx.util.GetFid;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.CustomLog;
import com.gljx.web.Entity.User;
import com.gljx.web.Entity.personData;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.CustomLogMapper;
import com.gljx.web.service.CustomLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class CustomLogServiceImpl extends CommonDao implements CustomLogService {

	@Autowired
	private CustomLogMapper customLogMapper;

    @Autowired
    private HttpSession session;

    /**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("Pindex", pindex);
		paramMap.put("Psql", psql);
		paramMap.put("Psize", psize);
		paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
		paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
		this.getSqlSession().select("com.gljx.web.dao.CustomLogMapper.pagemap", paramMap, null);
		return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("Psql", psql);
		paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
		this.getSqlSession().select("com.gljx.web.dao.CustomLogMapper.pagecount", paramMap, null);
		return paramMap;
	}

	@Override
	public void addLog(String fnameL2, String fdescriptionL2, String studentId,String fcreatorid,String... companyId) {
		// TODO Auto-generated method stub
        User user = (User) session.getAttribute("user");
        CustomLog customLog = new CustomLog();
		//fid
		customLog.setFid(GetFid.UUIDIntoFid());
		//创建时间
		customLog.setFcreatetime(TimeFormat.timeFormat(new Date()));
		//创建人id
		customLog.setFcreatorid(fcreatorid);
		//学生id
		customLog.setCfstudentid(studentId);
		//功能名称
		customLog.setFnameL2(fnameL2);
		//功能描述
		customLog.setFdescriptionL2(fdescriptionL2);
        //控制单元
		if(user == null){
			customLog.setFcontrolunitid(companyId[0]);
		}else{
			customLog.setFcontrolunitid(user.getFcompanyid());
		}
        //添加日志
		customLogMapper.insertSelective(customLog);
	}

	/**
	 * 查询编码
	 */
	public String selectFunmber(String tableName,String name,String companyId) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("v_tableName", tableName);
		paramMap.put("v_companyId", companyId);
		paramMap.put("v_funmber", java.sql.Types.VARCHAR);
		this.getSqlSession().select("com.gljx.web.dao.CustomLogMapper.selectFunmber", paramMap, null);
		String fnumber = null;
		if (paramMap.get("v_funmber") != null) {
			fnumber = paramMap.get("v_funmber").toString();
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		//获取当天
		String time = simpleDateFormat.format(new Date());
		StringBuffer stringBuffer = new StringBuffer();
		String number = null;
		//如果为空说明是第一次,字符串长度小于12
		if(fnumber == null || "".equals(fnumber) || fnumber.length()<12){
			number = "0001";
		}else{
			String numberTemp = fnumber.substring(fnumber.length()-12);
			String oldNumber = numberTemp.substring(8,12);
			Integer oldNumberForInt = Integer.valueOf(oldNumber);
			//生成四位字符串
			if (oldNumberForInt < 10) {
				if(oldNumberForInt == 9) {
					number = "0010";
				} else {
					number = "000" + (oldNumberForInt + 1);
				}
			} else if (oldNumberForInt < 100 && oldNumberForInt > 9 ) {
				if(oldNumberForInt == 99) {
					number = "0100";
				} else {
					number = "00" + (oldNumberForInt + 1);
				}
			} else if (oldNumberForInt < 1000 && oldNumberForInt > 99) {
				if(oldNumberForInt == 99) {
					number = "100";
				} else {
					number = "0" + (oldNumberForInt + 1);
				}
			} else {
				number = "0001";
			}
			/*//取编码最后的12位数
			String fnumberString = fnumber.substring(fnumber.length()-12);
			//取编码时间
			String timeString = fnumberString.substring(0,8);
			//如果是当天就进行+1操作，否则重新生成
			System.out.println(timeString);
			if(timeString.equals(time)){
				//取编码最后的4位数
				String num = fnumberString.substring(fnumberString.length()-4);
				//转成Integer类型
				Integer fnumbers = Integer.valueOf(num);
				//先进行+1操作在转成String
				if(fnumbers < 10){
					number = "000"+(fnumbers+1);
				}else if(fnumbers < 100){
					number = "00"+(fnumbers+1);
				}else if(fnumbers < 1000){
					number = "0"+(fnumbers+1);
				}else{
					number = ""+(fnumbers+1);
				}
			}else{
				number = "0001";
			}*/
		}
		//功能名
		stringBuffer.append(name);
		//时间
		stringBuffer.append(time);
		//编码
		stringBuffer.append(number);
		return stringBuffer.toString();
	}

	/**
	 * 根据学员ID查询日志
	 */
	@Override
	public List<CustomLog> selectCustomLogById(String fid) {
		// TODO Auto-generated method stub
		return customLogMapper.selectCustomLogById(fid);
	}

	@Override
	public List<personData> selectHealthyAll() {
		// TODO Auto-generated method stub
		return customLogMapper.selectHealthyAll();
	}

	@Override
	public List<personData> selectMarriageAll() {
		// TODO Auto-generated method stub
		return customLogMapper.selectMarriageAll();
	}

	@Override
	public List<personData> selectPoliticalAll() {
		// TODO Auto-generated method stub
		return customLogMapper.selectPoliticalAll();
	}

	@Override
	public List<personData> selectFamilyNameAll() {
		// TODO Auto-generated method stub
		return customLogMapper.selectFamilyNameAll();
	}

	@Override
	public List<personData> selectEdcationAll() {
		// TODO Auto-generated method stub
		return customLogMapper.selectEdcationAll();
	}

	@Override
	public List<String> queryCarTypeAll() {
		// TODO Auto-generated method stub
		return customLogMapper.selectCarTypeAll();
	}

    @Override
    public int insertCustomLog(List<CustomLog> customLogs) {
        return customLogMapper.insertCustomLog(customLogs);
    }

    @Override
    public List<CustomLog> selectCustomLogByInfo(String fcompanyid, String fnameL2, String fdescriptionL2, String beginTime, String endTime, String student) {
        Map<String, String> map = new HashMap<>();
        map.put("fcompanyid", fcompanyid);
        map.put("fnameL2", fnameL2);
        map.put("fdescriptionL2", fdescriptionL2);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("student", student);
        return customLogMapper.selectCustomLogByInfo(map);
    }

    @Override
    public void batchInsert(String[] studentName, String[] studentId, String meg, String function, String[]... cfstudentno) {
        List<CustomLog> customLogs = new ArrayList<>();
        User user = (User) session.getAttribute("user");
        for (int i = 0; i < studentName.length; i++) {
            CustomLog customLog = new CustomLog();
            customLog.setFid(GetFid.UUIDIntoFid());
            customLog.setFcreatetime(TimeFormat.timeFormat(new Date()));
            customLog.setFcreatorid(user.getFid());
            customLog.setCfstudentid(studentId[i]);
            customLog.setFnameL2(function);
            customLog.setFdescriptionL2("学员:" + cfstudentno[0][i] + studentName[i] + meg);
            //控制单元
            customLog.setFcontrolunitid(user.getFcompanyid());
            customLogs.add(customLog);
        }
        customLogMapper.insertCustomLog(customLogs);
    }

    @Override
    public void batchInsertState(String[] studentName, String[] studentId, String function, String[] cfstudentno, String meg, String operation) {
        List<CustomLog> customLogs = new ArrayList<>();
        User user = (User) session.getAttribute("user");
        for (int i = 0; i < studentName.length; i++) {
            CustomLog customLog = new CustomLog();
            customLog.setFid(GetFid.UUIDIntoFid());
            customLog.setFcreatetime(TimeFormat.timeFormat(new Date()));
            customLog.setFcreatorid(user.getFid());
            customLog.setCfstudentid(studentId[i]);
            customLog.setFnameL2(function);
            customLog.setFdescriptionL2(operation + "学员:" + cfstudentno[i] + studentName[i] + meg);
            //控制单元
            customLog.setFcontrolunitid(user.getFcompanyid());
            customLogs.add(customLog);
        }
        customLogMapper.insertCustomLog(customLogs);
    }

}
